import UserModel from "../../../../models/User";
import Crypt from "../../../../utils/Crypt";
import behaviors from "../../../../config/behaviors";

const userModel = new UserModel()

Page({
  behaviors: [behaviors],
  data: {
    app: {}
  },
  onLoad: function (options) {
    // 审核改手动了
    // const token = userModel.getToken()
    // if (!token) {
    //   this.onLogin()
    // }

    this.getSet()
  },

  // 获取基础设置
  getSet() {
    userModel.getSet().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      const { app } = data
      this.setData({ app })
    })
  },

  onDeny() {
    wx.redirectTo({
      url: '/pages/home/index'
    })
  },

  onAgreement() {
    wx.navigateTo({
      url: '/sub_main/pages/user/agreement/index'
    })
  },

  onLogin() {
    wx.login({
      success: res => {
        if (res.code && res.code.length > 1) {

          userModel.login({ code: res.code, referrer: userModel.getReferrer() }).then(response => {
            let { data } = response
            data = Crypt.parse(data)
            // console.log(data)
            userModel.setToken(data.token)
            userModel.setSessionKey(data.session_key)

            if (!data.mobile || data.mobile.length < 1) {
              // redirect get info
              wx.redirectTo({
                url: '/sub_main/pages/user/mobile/index'
              })
            } else {
              wx.switchTab({
                url: '/pages/center/index'
              })
            }



          })
        }
      }
    })
  }
});
